package com.tang.Web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest)servletRequest;
        //System.out.println(req.getSession().getAttribute("userName"));
        //String name=(String)req.getSession().getAttribute("userName");
        if(req.getSession().getAttribute("userName")==null){
            System.out.println("成功拦截");
            req.getRequestDispatcher("../login.jsp").forward(servletRequest,servletResponse);
        }else{
            System.out.println("登录成功");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
